from OWRpy import * 
import redRi18n
_ = redRi18n.get_(package = 'base')

import redRGUI 
import signals

class dissimilarity(OWRpy): 
    globalSettingsList = ['commit']
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)

        # Python variables
        self.data = {}
        self.RFunctionParam_object = ''
        
        #create R variables in the R session. 
        self.setRvariableNames(["dissimilarity"])
        
        #Inputs
        self.inputs.addInput('id0', _('R Dataframe Object'), signals.base.RDataFrame, self.processobject)
        
        #Outputs
        self.outputs.addOutput('id0', 'Dissimilarity', signals.base.RMatrix) # should output dissimilaroty signal
		
        #GUI
        self.redRdissimilarityMethod = redRGUI.base.comboBox(self.controlArea,  label = "Method: ",
        items = [('manhattan','Manhattan'),('euclidean','Euclidean'),
        ('canberra','Canberra'),('bray','Bray-Curtis'),('kulczynski','Kulczynski'),
        ('jaccard','Jaccard'),('gower','Gower*'),('altGower','Alternative Gower*'),
        ('morisita','Morisita'),('horn','Horn'),('raup','Raup'),
        ('binomial','Binomial'),('chao','Chao')],
        editable=False)#, callback = self.onTestChange)

        self.redRdisOptions = redRGUI.base.checkBox(self.controlArea,  label = "Options", 
        buttons = [('binary','Perform presence/absence standardization before analysis (using "decostand" function).'),
        ('diag','Compute diagonals.'),('upper','Return only the upper diagonal.'), 
        ('na.rm','Pairwise deletion of missing observations when computing dissimilarities.')],
        setChecked=None,orientation='vertical')
        
        #Commit
        self.commit = redRGUI.base.commitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignLeft, 
        callback = self.commitFunction, processOnInput=True)
        
    def processobject(self, data):
        if not self.require_librarys(["vegan"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.RFunctionParam_object=data.getData()
            self.data = data
            if self.commit.processOnInput():
                self.commitFunction()
        else:
            self.RFunctionParam_object=''
    def commitFunction(self):
        
        if unicode(self.RFunctionParam_object) == '':
            self.status.setText('No data.')
            return

        if self.redRdissimilarityMethod.currentId() == '':
            self.status.setText('No method selected.')
            return
    
        injection = []
        if 'binary' in self.redRdisOptions.getCheckedIds(): 
            string='binary=TRUE'
            injection.append(string)
        if 'diag' in self.redRdisOptions.getCheckedIds(): 
            string='diag=TRUE'
            injection.append(string)
        if 'upper' in self.redRdisOptions.getCheckedIds(): 
            string='upper=TRUE'
            injection.append(string)
        if 'na.rm' in self.redRdisOptions.getCheckedIds(): 
            string='na.rm=TRUE'
            injection.append(string)
        inj = ','.join(injection)
                
        print 'inj: ', inj

        self.R(self.Rvariables['dissimilarity']+'<-vegdist(x='+unicode(self.RFunctionParam_object)+
        ', method="'+unicode(self.redRdissimilarityMethod.currentId())+'", '+inj+')', processingNotice=True)      
        
        
        #self.R(self.Rvariables['dissimilarity']+'<-vegdist(x='+unicode(self.RFunctionParam_object)+', method="'+unicode(self.redRdissimilarityMethod.currentId())+'")')  
        
        self.newData = signals.base.RDataFrame(self, data = self.Rvariables["dissimilarity"], checkVal=False)
        self.rSend("id0", self.newData)
        
